#include "Form1.h"
#include <sstream>
Form1::Form1()
{


    //ctor

    /*RECT rc;
    GetClientRect(hWnd,&rc);*/

    /*tabs=new cTabs(this,5,5,rc.right-10,rc.bottom-10);
    tabs->SetAncre("bottom",true,10);
    tabs->SetAncre("right",true,10);

    tabs->addTab("Tab 1");
    tabs->addTab("Tab 2");
    tabs->addTab("Tab 3");
    tabs->addTab("Tab 4 - plutôt long");
    tabs->onglets[1]->SetEnable(false);*/

    /*cEdit * textEdit=new cEdit(this,0,10,150,24);
    textEdit->SetBackgroundColor(255,200,200);
    textEdit->SetFont("ARIAL",20,1,false,false);
    textEdit->SetFontColor(255,0,0);
    textEdit->SetAncre("left",false,0);
    textEdit->SetAncre("right",true,10);
    textEdit->SetAncre("top",true,10);*/


    /*textEdit->SetAncre("top",true,10);
    textEdit->SetAncre("left",true,10);*/

    /*cGroupBox * groupe=new cGroupBox(this,10,10,100,100,"test",tabs->onglets[2]->conteneur);
    groupe->SetAncre("right",true,10);
    groupe->SetAncre("bottom",true,10);

    bouton=new cBouton(this,"Mon bouton",0,55,100,20,groupe);
    bouton->OnRightClick=(functionEvenement)&Form1::buttonRClick;
    bouton->OnClick=(functionEvenement)&Form1::buttonClick;*/
}

void Form1:: OnCreate()
{
    this->SetSize(330,400);
    this->CenterWindow();
    this->SetBackgroundColor(249,255,254);
    this->SetTitle("Connexion");

	cLabel * Grimoires=new cLabel(this,36,18,241,59,"Grimoires");
    Grimoires->SetFont("Arial",48,FW_BOLD,true);
    Grimoires->SetFontColor(196,99,0);

    cLabel * LoginText=new cLabel(this,11,111,121,23,"Utilisateur :",NULL,RIGHT);
    LoginText->SetFontColor(0,0,138);
    LoginText->SetFont("Arial",18,FW_BOLD);

    Login=new cEdit(this,135,110,166,27);
    Login->SetBackgroundColor(255,255,223);
    Login->SetFontColor(0,0,138);
    Login->SetFont("Arial",18,FW_BOLD);
    Login->OnGetFocus=(functionEvenement)&Form1::editTextGetFocus;
    Login->OnKillFocus=(functionEvenement)&Form1::editTextKillFocus;
    Login->SetFocus();

    cLabel * MdpText=new cLabel(this,1,141,131,23,"Mot de passe :",NULL,RIGHT);
    MdpText->SetFontColor(0,0,138);
    MdpText->SetFont("Arial",18,FW_BOLD);

    Mdp=new cEdit(this,135,139,166,27);
    Mdp->SetBackgroundColor(255,255,223);
    Mdp->SetFontColor(0,0,138);
    Mdp->SetFont("Arial",18,FW_BOLD);
    Mdp->setPasswordChar('*');
    Mdp->OnGetFocus=(functionEvenement)&Form1::editTextGetFocus;
    Mdp->OnKillFocus=(functionEvenement)&Form1::editTextKillFocus;

    cBouton * accepter=new cBouton(this,"Accepter (rc)",134,237,163,35);
    accepter->SetFont("Arial",14,FW_BOLD);
    accepter->SetFontColor(0,0,138);
    accepter->OnClick=(functionEvenement)&Form1::buttonRClick;

    cBouton * quitter=new cBouton(this,"Quitter (esc)",134,274,163,35);
    quitter->SetFont("Arial",14,FW_BOLD);
    quitter->SetFontColor(0,0,138);
    quitter->OnClick=(functionEvenement)&Form1::buttonClick;


    cFichier * fichier=new cFichier(cUtils::getApplicationPath()+"config.ini");
    string nomClient=fichier->readConfigValue("Adressage","Client","");

    cLabel * ClientText=new cLabel(this,5,323,304,44,nomClient,NULL,MIDDLE);
    ClientText->SetFontColor(95,120,255);
    ClientText->SetFont("Arial",18,FW_BOLD);

    this->AjouterRaccourci(false,false,false,VK_RETURN, (functionEvenement)&Form1::buttonRClick);
    this->AjouterRaccourci(false,false,false,VK_ESCAPE, (functionEvenement)&Form1::buttonClick);
}


void Form1::RaccourciOK(cEvent * object)
{
    Alerte("Raccourci","test");
}


void Form1::editTextKillFocus(cEvent * object)
{
    cHwnd* edit=object->GetHwnd();
    ((cEdit*)edit)->SetBackgroundColor(255,255,223);
}

void Form1::editTextGetFocus(cEvent * object)
{
    cHwnd* edit=object->GetHwnd();
    ((cEdit*)edit)->SetBackgroundColor(255,255,127);
}

void Form1::buttonRClick(cEvent * object)
{

    cODBC * test=new cODBC("Grimoires");
    string path=cUtils::getApplicationPath();

    if(!test->InstallDriver(path.substr(0,path.size()-1),"ODBC4D.dll"))
        Alerte("Installation","Installation échouée.");

   if((Login->getText()!="") && (Mdp->getText()!=""))
    {

        test->SetAdresse("192.168.0.4",19812);
        test->SetLogin("Super_Utilisateur","maupiti");
        bool connected=false;
        string profil="";
        if(test->Connect())
        {
            string requete="SELECT profil FROM Utilisateurs WHERE UPPER(nom_connexion)=UPPER('"+Login->getText()+"') AND UPPER(mot_de_passe)=UPPER('"+Mdp->getText()+"')";
            if(test->Query(requete))
            {
                if(test->Fetch())
                {
                    profil=test->GetString(1);
                    connected=true;
                }
            }
        }
        else
            Alerte("Connexion",test->GetError());
        test->Disconnect();


        if(connected){
            /*cFichier * fichier=new cFichier("c:\\Grimoires\\login_mdp.ini");
            fichier->writeConfigValue("AutoReconnexion","login",Login->getText());
            fichier->writeConfigValue("AutoReconnexion","mdp",Mdp->getText());

            string cheminCourant=cUtils::getApplicationPath();
            cheminCourant+="config.ini";
            delete(fichier);
            fichier=new cFichier(cheminCourant);
            string cheminLancement=fichier->readConfigValue("Adressage","chemin","");
            string shortPath=cheminLancement.substr(0,cheminLancement.rfind('\\')+1);

            ShellExecute(NULL,"open",cheminLancement.c_str(),"",shortPath.c_str(),SW_SHOWDEFAULT);

            this->Close();*/

            Form2 * entre=new Form2(profil);
            entre->ReplaceCurrent(this);
        }
    }
    else
    {
        Alerte("Attention","Il manque une donnée !");
    }
}

void Form1::buttonClick(cEvent * object)
{
    QuitterApplication();
}

Form1::~Form1()
{
    //dtor
}
